package com.summer.safe.system.service;

import com.summer.safe.base.BaseService;
import com.summer.safe.base.Page;
import com.summer.safe.system.dao.UserDao;
import com.summer.safe.utils.UserUtils;
import com.summer.safe.config.annotation.DataFilter;
import com.summer.safe.system.entity.User;
import com.summer.safe.utils.Global;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户Service
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService extends BaseService<UserDao, User> {



    @Override
    public List<User> findList(User user) {
        return dao.findList(user);
    }

    @Override
    @DataFilter(type = Global.USER_DATE)
    public Page<User> findPage(Page<User> page, User user) {
        page=super.findPage(page, user);
        return page;
    }


    public void updatePassword(User user) {
        dao.updatePassword(user);
    }

   /* public void updatestate(String id,String state) {
        userDao.updatestate(id,state);
    }*/


    @Override
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void save(User entity) {
        entity.preInsert();
        dao.insert(entity);
    }

    public User getByLoginName(String loginName) {
        User user = dao.getByLoginName(loginName);

        return user;
    }

    @Override
    public void delete(User user) {
        //删除用户
        dao.delete(user);
        //删除用户关联的相关角色
        dao.deleteUserRole(user);
        UserUtils.clearCache(UserUtils.getUser());
    }
    public void updateUser(User user) {
        dao.update(user);
        UserUtils.clearCache(UserUtils.getUser());
    }
     public void updatePhoto(User user) {
        dao.updatePhoto(user);
         UserUtils.clearCache(UserUtils.getUser());
    }

}
